function outStru=assignXAxis(sampleVec,NTicks,sampleCell)
% Given a vector sampleVec 
% Generate the x Axis Labels by defining NTick-2 ticks between the year at which
% the sample begins and ends, and including 1 tick for each year at the
% beginning and end of sample. 
% 
% Output 
% .xtickValues    = Values to use 
% .xtickLabels    = Labels to use 
% .xtickPosInSamp = Position in the original Sample
% 
% AJ 1/9/2012
%
% Edited 8/8/2014 by TM
% Tick Marks were going beyond the length of the sample.  Fixed the code so
% that xtickEnd was defined as the floor of the last element in the 
% sampleVec rather than the ceiling of the last years. Also, added in that
% if the number of requested ticks is greater than the number of unique
% years, then it just uses the unique years.

%% 1. Define Years to Use 
outStru.xtickValues=zeros(NTicks,1);        % Initialize Tick Values
outStru.xtickValues(1)=ceil(sampleVec(1));  % Starting Tick Value
xtickEnd=floor(sampleVec(end));             % Ending Tick Value
years=floor(sampleVec);                     % End

if sum(unique(years)>=outStru.xtickValues(1))<NTicks             % Added TM: 8/8/2014
    outStru.xtickValues = unique(years(years>=outStru.xtickValues(1)));
else
    for ii=2:NTicks;
        outStru.xtickValues(ii)=round( outStru.xtickValues(ii-1)+ (xtickEnd-outStru.xtickValues(1))/(NTicks-1) );
    end
    if outStru.xtickValues(end) > floor( years(end) )
        outStru.xtickValues(end)=floor(years(end));
    end
    outStru.xtickValues=unique( outStru.xtickValues );
end
%% Define Tick Positions
NTicks=length(outStru.xtickValues); 
outStru.xtickPosInSamp=zeros(NTicks,1); 
for ii=1:NTicks
    outStru.xtickPosInSamp(ii)=find( sampleVec == outStru.xtickValues(ii) );
end
if nargin < 3
    outStru.xtickLabels=cell(NTicks,1);
    for ii=1:NTicks; 
        outStru.xtickLabels(ii)={sprintf('%4.0f',outStru.xtickValues(ii))}; 
    end 
else 
        outStru.xtickLabels=sampleCell(outStru.xtickPosInSamp); 
end 

end
